/**
  * Author: Mei
  * Date: 2020-12-29 11:59
  * Description: 
  *
  */
#include "iterator.h"

namespace detail_range {
    template<typename T>
    class impl {
    public:
        typedef T value_type;
        typedef const value_type &reference;
        typedef const value_type &const_reference;
        typedef const detail_range::iterator<value_type> iterator;  //对外接口类
        typedef const detail_range::iterator<value_type> const_iterator;
        typedef typename iterator::size_type size_type;

    private:
        const value_type begin_;
        const value_type end_;
        const value_type step_;
        const size_type max_count_; //迭代的次数

        //求出两个范围之间能够迭代的次数
        size_type get_adjusted_count(void) const {
            if (step_ > 0 && begin_ >= end_) {
                throw std::logic_error("End value must be greater than begin value");
            } else if (step_ < 0 && begin_ <= end_) {
                throw std::logic_error("End value must be less than begin value");
            }
            size_type x = static_cast<size_type>((end_ - begin_) / step_);
            if (begin_ + (step_ * x) != end_) {
                //std::cout << "shut" << std::endl;
                ++x;
            }
            return x;
        }

    public:
        impl(value_type begin_val, value_type end_val, value_type step_val) :
                begin_(begin_val),
                end_(end_val),
                step_(step_val),
                max_count_(get_adjusted_count()) {
        }

        size_type size(void) const {
            return max_count_;
        }

        const_iterator begin(void) const {
            return {0, begin_, step_};
        }

        const_iterator end(void) const {
            //std::cout << "impl," << max_count_ << "," << begin_ << "," << step_ << std::endl;
            return {max_count_, begin_, step_};
        }
    };
}